package com.cyz.service.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.cyz.dao.ProductDao;
import com.cyz.dao.impl.ProductDaoImpl;
import com.cyz.pojo.Product;
import com.cyz.service.ProductService;


public class ProductServiceImpl implements ProductService {

	private ProductDao productDao = new ProductDaoImpl();
	@Override
	public Product getProductById(Integer id) throws SQLException {
		ProductDao productDao = new ProductDaoImpl();
		Product product=null;
		product=productDao.getProductById(id);
		return product;
	}

	@Override
	public List<Product> getProductByTypeName(String typeName) {
		ProductDao productDao = new ProductDaoImpl();
		List<Product> productList = null;
		try {
			productList = productDao.getProductByTypeName(typeName);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return productList;
	}

	@Override
	public Product getProductByProductName(String ProductName) {
		// TODO Auto-generated method stub
		ProductDao productDao = new ProductDaoImpl();
		Product product = null;
		product = productDao.getProductByProductName(ProductName);
		return product;
	}
//////////////////////////
	@Override
	public void addType(String typeName) throws Exception {
		
		if(null != productDao.getTypeByName(typeName)) { //名称已存在
			throw new Exception();
		} else {
			productDao.addType(typeName);
		}
	}

	@Override
	public void deleteType(String typeName) {
		productDao.deleteType(typeName);
	}

	@Override
	public void updateType(String typeName, String newName) {
		productDao.updateType(typeName, newName);
	}

	@Override
	public String getTypeByName(String typeName) {
		return productDao.getTypeByName(typeName);
	}

	@Override
	public Integer getTypeIdByTypeName(String typeName) {
		return productDao.getTypeIdByTypeName(typeName);
	}

	@Override
	public void deleteProduct(String productName) {
		productDao.deleteProduct(productName);
	}

	@Override
	public void addProduct(Product product) {
		productDao.addProduct(product);
	}

	@Override
	public Product getProductByName(String productName) {
		return productDao.getProductByProductName(productName);
	}

	@Override
	public void updateProduct(Product product) {
		productDao.updateProduct(product);
	}

	@Override
	public Integer getProductIdByName(String productName) {
		return productDao.getProductIdByProductName(productName);
	}

	@Override
	public List<Product> getProductBypaging(Map<String, Object> map) {
		return null;
	}

	@Override
	public void renameType(Map map) throws Exception {
		
		if(null != productDao.getTypeByName(((String)map.get("newName")))) { //如果新名称已经存在
			throw new Exception();
		} else {
			productDao.updateType((String)map.get("typeName"),(String)map.get("newName"));;
		}
	}
	
	
}
